package FuenteDeDatos;

import java.util.Observable;
import java.sql.*;
import java.util.Observer;
public class DataSource extends Observable implements Observer {
    
    private String ip="localhost";
    private String db="";
    private String usuario="root";
    private String password="";
    private String codigosql;
    private Connection conexion=null;
    private Statement st=null;
    public ResultSet rs;
    private boolean aviso=false;
    private boolean aviso2=false;
    public int estado=0;
    private String Driver[] ={"org.gjt.mm.mysql.Driver","oracle.jdbc.driver.OracleDriver"};
 
    private  String conectar(){
         try{
          
             //otra mod
        if(st==null){
            //System.out.println("Se ha creado la conexion");
            System.out.println(getDriver()[0]);
              Class.forName(getDriver()[0]);
              if(getDriver()[0].equals("org.gjt.mm.mysql.Driver")){
                  System.out.println("Eligio MYSQL");
                    setConexion(DriverManager.getConnection("jdbc:mysql://"+getIp()+"/"+getDb(), getUsuario(), getPassword()));
         st=        getConexion().createStatement();
              }else{
                  System.out.println("Eligio Oracle");
                  //jdbc:oracle:thin:@IPADDRES:1521:INSTANCIA
                  System.out.println("jdbc:oracle:thin:@"+getIp());
                    setConexion(DriverManager.getConnection("jdbc:oracle:thin:@"+getIp(), getUsuario(), getPassword()));
                st= getConexion().createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);  
              }
         
            }
        }
        catch(Exception a)
        {
            //System.out.println("Error en Conexion");
            aviso = false;
            return "Error en Conexion";
        }
        //System.out.println("conexion exitosa");
            aviso = true;
        return "OK en Conexion";
    }

    public ResultSet consulta(){
        conectar();
        try {
            
           
            rs= st.executeQuery(this.getCodigosql());
           
            rs.next();
              
            this.setChanged();
            this.notifyObservers();
            this.aviso2=true;
            //cerramos el statement y la conexion
            //this.st.close();
            //this.conexion.close();

        } catch (SQLException ex) {
           System.out.println("error de consulta");

        }

        return rs;
    }

        public ResultSet consulta_det(String con){
        conectar();
        try {
            rs = st.executeQuery(con);
            rs.next();

            this.setChanged();
            this.notifyObservers();
            this.aviso2=true;
            rs = st.executeQuery(this.getCodigosql());
            //cerramos el statement y la conexion
            //this.st.close();
            //this.conexion.close();

        } catch (SQLException ex) {
           // System.out.println("error de consulta");

        }

        return rs;
    }

     public String ejecutarPA(String con){
        conectar();
        String res="N/A";
        try {
            rs = st.executeQuery(con);

            rs=st.executeQuery("select @r");
           if (rs.next()){
            res=rs.getString(1);
             //  System.out.println(rs.getString(1));
           }




        } catch (SQLException ex) {
           // System.out.println("error de consulta");

        }
 return res;

    }


    public SQLException consulta(String con){
        conectar();
        try {
             st.executeUpdate(con);
          
            //cerramos el statement y la conexion
            this.st.close();
            this.getConexion().close();

        } catch (SQLException ex) {
           // System.out.println("error de consulta");
            System.out.println(""+ex);
            return(ex);

        }
    return(null);
    }
 

    public void siguiente(){
        try {
           if(aviso2==true){
           if( rs.next()){
            this.setChanged();
            this.notifyObservers();
           }else{
            rs.last();
            }

           }else{
            consulta();

            this.setChanged();
            this.notifyObservers();

           }

        } catch (SQLException ex) {
           // System.out.println("error de consulta");

        }

    }
        public void anterior(){
        try {
           if(aviso2==true){
            if(rs.previous()){
            this.setChanged();
            this.notifyObservers();
            }else{
            rs.first();

            }
           }else{
            consulta();
            rs.previous();
            this.setChanged();
            this.notifyObservers();
           }


        } catch (SQLException ex) {
           // System.out.println("error de consulta");

        }

    }
        public void primero(){
        try {
           if(aviso2==true){
            if(rs.first()){
            this.setChanged();
            this.notifyObservers();}
           }else{
            consulta();
            rs.first();
            this.setChanged();
            this.notifyObservers();
           }


        } catch (SQLException ex) {
           // System.out.println("error de consulta");

        }

    }
         public void ultimo(){
        try {
           if(aviso2==true){
            if(rs.last()){
            this.setChanged();
            this.notifyObservers();}
           }else{
            consulta();
            rs.last();
            this.setChanged();
            this.notifyObservers();
           }


        } catch (SQLException ex) {
           // System.out.println("error de consulta");

        }

    }

public ResultSet consultaSN(String con){
        conectar();
        try {
            rs = st.executeQuery(con);
            rs.next();
        } catch (SQLException ex) {
           // System.out.println("error de consulta");

        }

        return rs;
        
    }

public void Desactivar(){
    estado=1;
     this.setChanged();
     this.notifyObservers();
}
public void Activar(){
    estado=0;
     this.setChanged();
     this.notifyObservers();

}




    /**
     * @return the ip
     */
    public String getIp() {
        return ip;
    }

    /**
     * @param ip the ip to set
     */
    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * @return the db
     */
    public String getDb() {
        return db;
    }

    /**
     * @param db the db to set
     */
    public void setDb(String db) {
        this.db = db;
    }

    /**
     * @return the usuario
     */
    public String getUsuario() {
        return usuario;
    }

    /**
     * @param usuario the usuario to set
     */
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the codigosql
     */
    public String getCodigosql() {
        return codigosql;
    }

    /**
     * @param codigosql the codigosql to set
     */
    public void setCodigosql(String codigosql) {
        this.codigosql = codigosql;
    }

    /**
     * @return the Driver
     */
    public String[] getDriver() {
        return Driver;
    }

    /**
     * @param Driver the Driver to set
     */
    public void setDriver(String[] Driver) {
        this.Driver = Driver;
    }

    /**
     * @return the conexion
     */
    public Connection getConexion() {
        return conexion;
    }

    /**
     * @param conexion the conexion to set
     */
    public void setConexion(Connection conexion) {
        this.conexion = conexion;
    }

    public void update(Observable o, Object o1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
